package cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlainP;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.biz.controller.admin.emergencyReservePlainP.vo.*;
import cn.iocoder.yudao.module.biz.convert.emergencyReservePlainP.BizEmergencyReservePlainPConvert;
import cn.iocoder.yudao.module.biz.dal.dataobject.emergencyReservePlainP.BizEmergencyReservePlainPDO;
import cn.iocoder.yudao.module.biz.service.emergencyReservePlainP.BizEmergencyReservePlainPService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 - 应急人员")
@RestController
@RequestMapping("/biz/emergency-reserve-plain-p")
@Validated
public class BizEmergencyReservePlainPController {

    @Resource
    private BizEmergencyReservePlainPService emergencyReservePlainPService;

    @PostMapping("/create")
    @ApiOperation("创建应急人员")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-p:create')")
    public CommonResult<String> createEmergencyReservePlainP(@Valid @RequestBody BizEmergencyReservePlainPCreateReqVO createReqVO) {
        return success(emergencyReservePlainPService.createEmergencyReservePlainP(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新应急人员")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-p:update')")
    public CommonResult<Boolean> updateEmergencyReservePlainP(@Valid @RequestBody BizEmergencyReservePlainPUpdateReqVO updateReqVO) {
        emergencyReservePlainPService.updateEmergencyReservePlainP(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除应急人员")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-p:delete')")
    public CommonResult<Boolean> deleteEmergencyReservePlainP(@RequestParam("id") String id) {
        emergencyReservePlainPService.deleteEmergencyReservePlainP(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得应急人员")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-p:query')")
    public CommonResult<BizEmergencyReservePlainPRespVO> getEmergencyReservePlainP(@RequestParam("id") String id) {
        BizEmergencyReservePlainPDO emergencyReservePlainP = emergencyReservePlainPService.getEmergencyReservePlainP(id);
        return success(BizEmergencyReservePlainPConvert.INSTANCE.convert(emergencyReservePlainP));
    }

    @GetMapping("/list")
    @ApiOperation("获得应急人员列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-p:query')")
    public CommonResult<List<BizEmergencyReservePlainPRespVO>> getEmergencyReservePlainPList(@RequestParam("ids") Collection<String> ids) {
        List<BizEmergencyReservePlainPDO> list = emergencyReservePlainPService.getEmergencyReservePlainPList(ids);
        return success(BizEmergencyReservePlainPConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得应急人员分页")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-p:query')")
    public CommonResult<PageResult<BizEmergencyReservePlainPRespVO>> getEmergencyReservePlainPPage(@Valid BizEmergencyReservePlainPPageReqVO pageVO) {
        PageResult<BizEmergencyReservePlainPDO> pageResult = emergencyReservePlainPService.getEmergencyReservePlainPPage(pageVO);
        return success(BizEmergencyReservePlainPConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出应急人员 Excel")
    @PreAuthorize("@ss.hasPermission('biz:emergency-reserve-plain-p:export')")
    @OperateLog(type = EXPORT)
    public void exportEmergencyReservePlainPExcel(@Valid BizEmergencyReservePlainPExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BizEmergencyReservePlainPDO> list = emergencyReservePlainPService.getEmergencyReservePlainPList(exportReqVO);
        // 导出 Excel
        List<BizEmergencyReservePlainPExcelVO> datas = BizEmergencyReservePlainPConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "应急人员.xls", "数据", BizEmergencyReservePlainPExcelVO.class, datas);
    }

    @GetMapping("/list/all")
    @ApiOperation("获得所有应急人员列表")
    public CommonResult<List<BizEmergencyReservePlainPRespVO>> getEmergencyReservePlainPListAll() {
        List<BizEmergencyReservePlainPDO> list = emergencyReservePlainPService.getAll();
        return success(BizEmergencyReservePlainPConvert.INSTANCE.convertList(list));
    }

}
